# -*- Mode: makefile -*-
#
# TCG tests
#
# These are complicated by the fact we want to build them for guest
# systems. This requires knowing what guests we are building and which
# ones we have cross-compilers for or docker images with
# cross-compilers.
#
# The tests themselves should be as minimal as possible as
# cross-compilers don't always have a large amount of libraries
# available.
#
# We only include the host build system for SRC_PATH and we don't
# bother with the common rules.mk. We expect the following:
#
#   CC - the C compiler command
#   EXTRA_CFLAGS - any extra CFLAGS
#   BUILD_STATIC - are we building static binaries
#
# By default all tests are statically compiled but some host systems
# may not package static libraries by default. If an external
# cross-compiler can only build dynamic libraries the user might need
# to make extra efforts to ensure ld.so can link at runtime when the
# tests are run.
#
# We also accept SPEED=slow to enable slower running tests
#
# We also expect to be in the tests build dir for the FOO-(linux-user|softmmu).
#

all:
-include ../config-host.mak
-include config-target.mak

# Get semihosting definitions for user-mode emulation
ifeq ($(filter %-softmmu, $(TARGET)),)
-include $(SRC_PATH)/configs/targets/$(TARGET).mak
endif

# for including , in command strings
COMMA := ,
NULL :=
SPACE := $(NULL) #
TARGET_PREFIX=tests/tcg/$(TARGET):$(SPACE)

quiet-@ = $(if $(V),,@$(if $1,printf "  %-7s %s\n" "$(strip $1)" "$(strip $2)" && ))
quiet-command = $(call quiet-@,$2,$3)$1

cc-test = $(CC) -Werror $1 -c -o /dev/null -xc /dev/null >/dev/null 2>&1
cc-option = if $(call cc-test, $1); then \
    echo "$(TARGET_PREFIX)$1 detected" && echo "$(strip $2)=y" >&3; else \
    echo "$(TARGET_PREFIX)$1 not detected"; fi

# $1 = test name, $2 = cmd, $3 = desc
ifeq ($(filter %-softmmu, $(TARGET)),)
run-test = $(call quiet-command, timeout --foreground $(TIMEOUT) $2 > $1.out, \
	TEST,$(or $3, $*, $<) on $(TARGET_NAME))
else
run-test = $(call quiet-command, timeout --foreground $(TIMEOUT) $2, \
        TEST,$(or $3, $*, $<) on $(TARGET_NAME))
endif

# $1 = test name, $2 = reference
# to work around the pipe squashing the status we only pipe the result if
# we know it failed and then force failure at the end.
diff-out = $(call quiet-command, diff -q $1.out $2 || \
                                 (diff -u $1.out $2 | head -n 10 && false), \
                                 DIFF,$1.out with $2)

# $1 = test name, $2 = reason
skip-test = @printf "  SKIPPED %s on $(TARGET_NAME) because %s\n" $1 $2

# $1 = test name, $2 = reference
# As above but only diff if reference file exists, otherwise the test
# passes if it managed to complete with a status of zero
conditional-diff-out = 							\
	$(if $(wildcard $2), 						\
		$(call diff-out,$1,$2), 				\
		$(call skip-test,"$1 check","no reference"))


# Tests we are building
TESTS=
# additional tests which may re-use existing binaries
EXTRA_TESTS=

# Start with a blank slate, the build targets get to add stuff first
CFLAGS=
LDFLAGS=

QEMU_OPTS=


# If TCG debugging, or TCI is enabled things are a lot slower
# ??? Makefile no longer has any indication that TCI is enabled,
# but for the record:
#   15s    original default
#   60s    with --enable-debug
#   90s    with --enable-tcg-interpreter
TIMEOUT=90

ifeq ($(filter %-softmmu, $(TARGET)),)
# The order we include is important. We include multiarch first and
# then the target. If there are common tests shared between
# sub-targets (e.g. ARM & AArch64) then it is up to
# $(TARGET_NAME)/Makefile.target to include the common parent
# architecture in its VPATH.
-include $(SRC_PATH)/tests/tcg/multiarch/Makefile.target
-include $(SRC_PATH)/tests/tcg/$(TARGET_NAME)/Makefile.target

# Add the common build options
CFLAGS+=-Wall -Werror -O0 -g -fno-strict-aliasing
ifeq ($(BUILD_STATIC),y)
LDFLAGS+=-static
endif

%: %.c
	$(CC) $(CFLAGS) $(EXTRA_CFLAGS) $< -o $@ $(LDFLAGS)
else
# For softmmu targets we include a different Makefile fragement as the
# build options for bare programs are usually pretty different. They
# are expected to provide their own build recipes.
EXTRA_CFLAGS += -ffreestanding
-include $(SRC_PATH)/tests/tcg/minilib/Makefile.target
-include $(SRC_PATH)/tests/tcg/multiarch/system/Makefile.softmmu-target
-include $(SRC_PATH)/tests/tcg/$(TARGET_NAME)/Makefile.softmmu-target

endif

all: $(TESTS) $(EXTRA_TESTS)

#
# Test Runners
#
# By default we just run the test with the appropriate QEMU for the
# target. More advanced tests may want to override the runner in their
# specific make rules. Additional runners for the same binary should
# be added to EXTRA_RUNS.
#

RUN_TESTS=$(patsubst %,run-%, $(TESTS))

# If plugins exist also include those in the tests
ifeq ($(CONFIG_PLUGIN),y)
PLUGIN_SRC=$(SRC_PATH)/tests/plugin
PLUGIN_LIB=../../plugin
VPATH+=$(PLUGIN_LIB)
PLUGINS=$(patsubst %.c, lib%.so, $(notdir $(wildcard $(PLUGIN_SRC)/*.c)))

# We need to ensure expand the run-plugin-TEST-with-PLUGIN
# pre-requistes manually here as we can't use stems to handle it. We
# also add some special helpers the run-plugin- rules can use bellow.

$(foreach p,$(PLUGINS), \
	$(foreach t,$(TESTS),\
		$(eval run-plugin-$(t)-with-$(p): $t $p) \
		$(eval RUN_TESTS+=run-plugin-$(t)-with-$(p))))
endif

strip-plugin = $(wordlist 1, 1, $(subst -with-, ,$1))
extract-plugin = $(wordlist 2, 2, $(subst -with-, ,$1))

RUN_TESTS+=$(EXTRA_RUNS)

ifeq ($(filter %-softmmu, $(TARGET)),)
run-%: %
	$(call run-test, $<, $(QEMU) $(QEMU_OPTS) $<)

run-plugin-%:
	$(call run-test, $@, $(QEMU) $(QEMU_OPTS) \
		-plugin $(PLUGIN_LIB)/$(call extract-plugin,$@) \
		-d plugin -D $*.pout \
		 $(call strip-plugin,$<))
else
run-%: %
	$(call run-test, $<, \
	  $(QEMU) -monitor none -display none \
		  -chardev file$(COMMA)path=$<.out$(COMMA)id=output \
		  $(QEMU_OPTS) $<)

run-plugin-%:
	$(call run-test, $@, \
	  $(QEMU) -monitor none -display none \
		  -chardev file$(COMMA)path=$@.out$(COMMA)id=output \
	   	  -plugin $(PLUGIN_LIB)/$(call extract-plugin,$@) \
	    	  -d plugin -D $*.pout \
		  $(QEMU_OPTS) $(call strip-plugin,$<))
endif

gdb-%: %
	gdb --args $(QEMU) $(QEMU_OPTS) $<

.PHONY: run
run: $(RUN_TESTS)

clean:
	rm -f $(TESTS) *.o $(CLEANFILES)

distclean:
	rm -f config-cc.mak config-target.mak ../config-$(TARGET).mak
